﻿using BusinessLayer.VDataEntities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TrialManagmentTool.Helper;

namespace TrialManagementTool.Helper
{
    public class AuthenticationAttribute : AuthorizeAttribute
    {
        public override void OnAuthorization(AuthorizationContext filterContext)
        {

            if (HttpContext.Current.Session == null)// not login in any more;
            {
                if (string.IsNullOrEmpty(UserHelper.GetUserInfoFromCookie()))
                {
                    LogHelper.Info(string.Format("Request url:{0} fail, no user info found.", filterContext.HttpContext.Request.Url, UserHelper.GetUserInfoFromCookie()));
                    LogHelper.Info(string.Format("Request host name:{0},host ip:{1},url refer:{2}", HttpContext.Current.Request.UserHostName, HttpContext.Current.Request.UserHostAddress, HttpContext.Current.Request.UrlReferrer));
                    LogHelper.Info(HttpContext.Current.Request.ServerVariables.ToString());
                    LogHelper.Info("CookieInfomation:" + HttpContext.Current.Request.ServerVariables.Get("Http_Cookie"));
                    ProcessNotLogin(filterContext);
                }
            }
            else
            {
                if (HttpContext.Current.Session["AdminInfo"] as VDataEntity == null && string.IsNullOrEmpty(UserHelper.GetUserInfoFromCookie()))
                {
                    VDataEntity CurrentUser = new VDataEntity();
#if (DEBUG)
                    {
                        if (string.IsNullOrEmpty(CurrentUser.Name))
                        {
                            CurrentUser = new VDataEntity { ID = 1, Name = "TestAdmin" };
                            System.Web.HttpContext.Current.Session["AdminInfo"] = CurrentUser;
                        }
                        else
                        {
                            CurrentUser = System.Web.HttpContext.Current.Session["AdminInfo"] as VDataEntity;
                        }
                    }
#else
                    {
                        LogHelper.Info(string.Format("Request url:{0} fail, no user info found.", filterContext.HttpContext.Request.Url));
                        LogHelper.Info(string.Format("Request host name:{0},host ip:{1},url refer:{2}", HttpContext.Current.Request.UserHostName, HttpContext.Current.Request.UserHostAddress, HttpContext.Current.Request.UrlReferrer));
                        LogHelper.Info(HttpContext.Current.Request.ServerVariables.ToString());
                        LogHelper.Info("CookieInfomation:" + HttpContext.Current.Request.ServerVariables.Get("Http_Cookie"));

                        ProcessNotLogin(filterContext);
                    }
#endif
                }
            }
        }

        void ProcessNotLogin(AuthorizationContext filterContext, bool bfillresult = true)
        {
            filterContext.Result = new RedirectResult("~/Home/Login");
        }
    }
}
